{% extends 'IcsocUIBundle::page.html.twig' %}
{% block stylesheet %}
    {{ parent() }}
    {% stylesheets
    '@gritter_css'
    filter='cssrewrite' filter='uglifycss'
    %}
    <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}
{% block content %}
     <div class="main-container">
        <div class="page-content">
        <form class="form-horizontal" id="busy_form" action="{{ path('icsoc_agentstat_save') }}" method="post">
            {% for key,val in data %}
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right"> {{ key+1 }}. </label>
                    <div class="col-sm-3">
                        <input type="text" value="{{ val.stat_reason }}" name="option[]" placeholder="" class="col-xs-12 col-sm-12" maxlength="8" />
                    </div>
                    <button type="button" name="btn-minus" class="btn btn-sm btn-danger">
                        <span class="glyphicon glyphicon-minus"></span>
                    </button>
                </div>
            {% else %}
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right"> 1. </label>
                    <div class="col-sm-3">
                        <input type="text" name="option[]" placeholder="" class="col-xs-12 col-sm-12" maxlength="8" />
                    </div>
                    <button type="button" name="btn-minus" class="btn btn-sm btn-danger">
                        <span class="glyphicon glyphicon-minus"></span>
                    </button>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right"> 2. </label>
                    <div class="col-sm-3">
                        <input type="text" name="option[]" placeholder="" class="col-xs-12 col-sm-12" maxlength="8" />
                    </div>
                    <button type="button" name="btn-minus" class="btn btn-sm btn-danger">
                        <span class="glyphicon glyphicon-minus"></span>
                    </button>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right"> 3. </label>
                    <div class="col-sm-3">
                        <input type="text" name="option[]" placeholder="" class="col-xs-12 col-sm-12" maxlength="8" />
                    </div>
                    <button type="button" name="btn-minus" class="btn btn-sm btn-danger">
                        <span class="glyphicon glyphicon-minus"></span>
                    </button>
                </div>
            {% endfor %}
            <div class="clearfix form-actions">
                <div class="col-md-offset-3 col-md-9">
                    <button type="button" class="btn" id="add_option">
                        <span class="ace-icon fa fa-plus-circle"></span> {{ 'add'|trans }}
                    </button>
                    <button type="button" class="btn btn-primary" id="save_reason">
                        <span class="ace-icon fa fa-save"></span> {{ 'Save Setting'|trans }}
                    </button>
                </div>
            </div>
            {% for key,val in data %}
                <input type="hidden" name="old_option[{{ val.id }}]" value="{{ val.stat_reason }}">
            {% endfor %}
        </form>
        </div>
        </div>
{% endblock %}
{% block footer %}
    {% javascripts
    '@jqvalidate_js'
    '@common_js'
    '@gritter_js'
    filter='uglifyjs'
    %}
    <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
    <script>
        $(document).ready(function() {
            /** 删除 */
            $('button[name="btn-minus"]').on('click', function() {
                $(this).parent().remove();
                resetNumber();
            });
            /** 添加 */
            $('#add_option').on('click', function() {
                var length = $('#busy_form').children('.form-group').length;
                if (length < 5) {
                    var number = length+1;
                    var $content = $('<div class="form-group">\
                            <label class="col-sm-3 control-label no-padding-right"> '+number+'. </label>\
                    <div class="col-sm-3">\
                            <input type="text" name="option[]" placeholder="" class="col-xs-12 col-sm-12" maxlength="8" />\
                            </div>\
                            <button type="button" name="btn-minus" class="btn btn-sm btn-danger">\
                            <span class="glyphicon glyphicon-minus"></span>\
                            </button>\
                            </div>');
                    $content.insertBefore($('.form-actions'));
                    $('button[name="btn-minus"]').on('click', function() {
                        $(this).parent().remove();
                        resetNumber();
                    });
                } else {
                    gritterAlert("{{ 'Alert'|trans }}", "{{ 'Busy reasons exceed'|trans }}", 'error');
                }

                resetNumber();
            });
            /** 保存 */
            $('#save_reason').on('click', function(){
                $(this).addClass('disabled');
                var option = [];

                $('input[name="option[]"]').each(function(){
                    option.push($(this).val());
                });

                var data = option.sort();

                for(var i=0;i<option.length;i++){
                    if ($.trim(data[i]) == '') {
                        $(this).removeClass('disabled');
                        gritterAlert("{{ 'Alert'|trans }}", "{{ 'Busy reason cannot be empty'|trans }}", 'error');
                        return false;
                    }
                    if ($.trim(data[i]) == $.trim(data[i+1])){
                        $(this).removeClass('disabled');
                        gritterAlert("{{ 'Alert'|trans }}", "{{ 'Busy reason cannot be repeated'|trans }}", 'error');
                        return false;
                    }
                }

                $('#busy_form').submit();
            });
        });

        function resetNumber()
        {
            $("#busy_form").children('.form-group').each(function(index){
                var key = index+1;
                $(this).find('label').html(key+'.');
            });
        }
    </script>
{% endblock %}